<?php

namespace app\admin\controller\separate_accounts;

use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\model\Store;
use app\admin\model\StoreSub;
use app\api\model\SeparateSet;
use app\common\controller\Backend;
use fast\Http;
use think\cache\driver\Redis;
use think\Request;
use think\Session;
use wechat\MiniProgramService;
use wechat\WechatController;

class SubMch extends Backend
{
    protected $searchFields = 'id,name';
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new StoreSub();
        $this->service_secret = Session::get('admin')['secret'];
        if (empty($this->service_secret))
            $this->service_secret = "Jp)GuX=M^KthZV7^";
    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['store','storegroup'])
                ->where($where)
                ->where(['store_sub.service_secret'=>$this->service_secret])
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
    public function add()
    {
        $this->extracted();
        return parent::add();
    }
    public function edit($ids = null)
    {
        $this->extracted();
        return parent::edit($ids);
    }

    /**
     * @return void
     * @throws \think\exception\DbException
     */
    private function extracted(): void
    {
        if ($this->request->isAjax()) {
            $params = $this->request->param("row/a");
            if ($params['state'] == 1) {
                // 判断下是否需要传
                if ($params['type'] == "MERCHANT_ID" && empty($params['full_name']))
                    $this->error("类型为商户需要传分账接收方全称");
                // 如果这个等于1 则是要走商户绑定流程
                $store = Store::get(['id' => $params['store_id']]);
                $secret = SeparateSet::get(['secret'=>$this->service_secret]);
                $wechat = new WechatController($store['sub_mch_id'],$secret);
                $res = $wechat->addReceiverOpenid($params);
                if ($res['result_code'] == "FAIL") {
                    $this->error($res['err_code_des']);
                }
            }
        }
    }
    public function bing_user(){
        $secret = SeparateSet::get(['secret'=>$this->service_secret]);
        $min = new MiniProgramService($secret);
        $res = $min->access_token();
        $arr = json_decode($res['data'],true);
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$arr['access_token']}";
        // 参数
        $param = array();
        $param['expire_seconds'] = 3600 * 24; //有效时间
        $param['action_name'] = 'QR_SCENE'; //二维码类型
        $param['action_info']['scene']['scene_id'] = 123;  //二维码详细信息
        $res = Http::post($url,$param);
        var_dump($res);
    }
}